package cn.eoeit.mmy.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import cn.eoeit.mmy.model.UserInfo;
import cn.eoeit.mmy.service.ISessionService;

@Service("sessionService")
public class SessionServiceImpl implements ISessionService {
    
	public boolean isLogged(HttpServletRequest request){
		return getLoginInfo(request)!=null;
	}

	public void storeLoginInfo(HttpServletRequest request,UserInfo customer) {
		request.getSession().setAttribute(LOGIN_SESSION_KEY, customer);
	}
	
	public UserInfo getLoginInfo(HttpServletRequest request) {
		return (UserInfo)request.getSession().getAttribute(LOGIN_SESSION_KEY);
	}
	
	@SuppressWarnings("unchecked")
	public boolean hasRole(HttpServletRequest request,String role) {
		List<String> menuKeys = (List<String>)request.getSession().getAttribute(SESSION_MENU_KEYS);
		if(menuKeys == null) {
			return false;
		}
		return menuKeys.contains(role);
	}

    public void clear(HttpServletRequest request){
		request.getSession().removeAttribute(LOGIN_SESSION_KEY);
	}
}
